package android.magical;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.provider.BaseColumns;
import android.util.Log;
import android.widget.Toast;

public class DatabaseControl 
{
	public static final String KEY_OWNER = "owner";
	public static final String KEY_DATE = "date";
	public static final String KEY_TIME = "time";
	public static final String KEY_MESSAGE = "message";
	public static final int INSERT_ERROR = -1;
	
	public static final String TABLE_NAME = "magicalevent";
	
	private Context context;
	private SQLiteDatabase database;
	private DatabaseHelper dbHelper;
	
	
	public DatabaseControl(Context context)
	{
		this.context = context;
	}
	
	public DatabaseControl open() throws SQLiteException
	{
		dbHelper = new DatabaseHelper(context);
		database = dbHelper.getWritableDatabase();
		return this;
	}
	
	public void close()
	{
		dbHelper.close();
	}
	
	public boolean isOpen(){
		return database.isOpen();
	}
	
	public long addItem(String owner,String date, String time, String message)
	{
		ContentValues values = createContentValues(owner,date,time,message);
		return database.insert(TABLE_NAME, null, values);
	}
	
	public void deleteItem(int id){
		database.delete(TABLE_NAME, BaseColumns._ID + "= ?", new String[]{String.valueOf(id)});
	}
	
	public Cursor getItem(String date)
	{
		Cursor dataCursor = null;
		
		try
		{
			dataCursor = database.query(TABLE_NAME, new String[]{BaseColumns._ID, KEY_OWNER,KEY_MESSAGE,KEY_TIME}, KEY_DATE + "='"+date+"'", null, null, null, null);
		}
		catch(SQLiteException e)
		{
			Log.i("DatabaseError", "There seems to be a problem in getting the data.");
		}
		
		return dataCursor;
	}
	
	/**
	 * Checks if there are some events for a day
	 * @param date the date of the day
	 * @return 0 if no events, 1 if there are only personal events, 2 if there are only shared events
	 * and 3 if there are the both
	 */
	public int hasItem(String date, String owner)	{
		int res = 0;
		Cursor dataCursor = null;
		dataCursor = database.query(TABLE_NAME, new String[]{KEY_OWNER, KEY_MESSAGE}, KEY_DATE + "='"+date+"'", null, null, null, null);
		if(dataCursor!=null) {
			dataCursor.moveToFirst();
			for (int i=0 ; i<dataCursor.getCount() ; i++){
				Log.v("DB", "Checking if personal event");
				if (dataCursor.getString(0).equals(owner)){
					Log.v("DB", dataCursor.getString(1));
					Log.v("DB", "Result: personal");
					if (res==0) {res = 1;}
					else if (res==2) {
						dataCursor.close();
						return res = 3;
					}
				}
				else {
					Log.v("DB", "Result: shared");
					if (res==0) {res = 2;}
					else if(res==1) {
						dataCursor.close();
						return res = 3;
					}
				}
				dataCursor.moveToNext();
			}
		}
		dataCursor.close();
		return res;
	}

	public ContentValues createContentValues(String owner, String date,
			String time, String message) {
		ContentValues values = new ContentValues();
		values.put(KEY_OWNER, owner);
		values.put(KEY_DATE, date);
		values.put(KEY_TIME, time);
		values.put(KEY_MESSAGE, message);
		
		return values;
	}
	
}
